package com.example.template.controller;

import com.example.template.common.api.ApiCodeConstant;
import com.example.template.common.api.ApiResult;
import com.example.template.common.exception.GlobalException;
import com.example.template.dto.RoleDTO;
import com.example.template.dto.UserDTO;
import com.example.template.entity.Role;
import com.example.template.service.PermissionService;
import com.example.template.service.RoleService;
import com.example.template.utils.JsonUtil;
import com.example.template.vo.RoleVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: RoleController
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2024/02/19 16:19
 **/
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/getAllRole")
    public ApiResult getAllRole() {
        List<Role> roleList = roleService.getAllRole();
        List<RoleVO> roleVOList = new ArrayList<>();
        if (!roleList.isEmpty()) {
            for (Role r : roleList) {
                RoleVO rv = new RoleVO();
                BeanUtils.copyProperties(r, rv);
                rv.setPerms(permissionService.getPermIdsByRoleId(r.getRoleId()).toString());
                roleVOList.add(rv);
            }
        }
        return ApiResult.success(roleVOList);
    }

    @PostMapping("/updateRole")
    public ApiResult updateRole(@RequestBody @Validated RoleDTO roleDTO) {
        roleService.updateRole(roleDTO);
        return ApiResult.success("更新成功");
    }




}
